﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;

namespace TableParsers
{
    public class BookTableEntry
    {
        public ClassName ClassName = null;
        public ClassTime ClassTime = null;
        public bool Available = false;
        public int ClassId = 0;

    }

    public class BookTable: List<BookTableEntry>
    {
        public static BookTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebPageParser.WebPageCache pageCache = new WebPageParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }
        public static BookTable Fetch(WebPageParser.WebPageCache pageCache)
        {
            WebPageParser.BookWebPage webPage = pageCache.GetPage<WebPageParser.BookWebPage>();            

            BookTable bookTable = new BookTable();
            foreach (WebPageParser.BookClassesTableEntry webEntry in webPage.TableContent)
            {
                BookTableEntry memEntry = new BookTableEntry();
                memEntry.ClassName = ClassNameParser.ParseClassName(webEntry.Class);
                memEntry.ClassTime = ClassTimeParser.ParseBookEntry(webEntry.Date);

                string str = webEntry.Availability;
                if (str.ToLower().Equals("yes"))
                {
                    memEntry.Available = true;
                }
                else
                {
                    memEntry.Available = false;
                }

                memEntry.ClassId = Int32.Parse(webEntry.Book);
                bookTable.Add(memEntry);
            }

            return bookTable;
        }
    }
}
